package staffManager;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Bean.SqlBean;
import loginRegister.LoginBean;

/**
 *
 * @author 黄忠
 */
public class LookStaffServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        HttpSession session=request.getSession();
        LoginBean login=(LoginBean)session.getAttribute("login");
        if(login==null){
            response.sendRedirect("http://localhost:8080/login.jsp");
        }else {
            String sql = "select * from user";
            try {
                ResultSet rs = null;
                SqlBean sb = new SqlBean();
                sb.connectSQL();
                rs = sb.executeQuery(sql);
                ArrayList staffslist = null;
                if (session.getAttribute("staffslist") == null) {
                    staffslist = new ArrayList();
                    while (rs.next()) {
                        if(!login.getUserName().equals(rs.getString("userName"))) {
                            StaffBean ff = new StaffBean(rs);
                            if(!ff.getLevel().equals("超级管理员"))
                                staffslist.add(ff);
                        }
                    }
                    sb.closeSQL();
                    session.setAttribute("staffslist", staffslist);
                }
                response.sendRedirect("http://localhost:8080/staffManager/lookStaff.jsp");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        doGet(request, response);
    }

}